<?php
class UserAction extends BaseAction{
	
	  
    public function userList(){
    	$type = 'User';
    	if($_GET['type']){
    		$type = $_GET['type'];
    	}
    	import("ORG.Util.Page"); // 导入分页类   
      $count = M('User')->field('id')->where("type='".$type."'")->count(); // 查询满足要求的总记录数   
      $Page  = new Page($count,10); // 实例化分页类 传入总记录数和每页显示的记录数   
      $show  = $Page->show(); // 分页显示输出   
      $list  = M('User')->where("type='".$type."'")->order('ctime desc')->limit($Page->firstRow.','.$Page->listRows)->select();   
      $this->assign('list',$list); // 赋值数据集   
      $this->assign('page',$show); // 赋值分页输出
      $this->assign('type',$type);
    	
      $this->display();
    }
    
    public function add(){
    	$this->display();
    }
    
    public function doUser(){
    	
				$user = D('User');
				if($user->create()){
					$user->password = md5($user->password);
					$user->ctime = time();
					$user->etime = time();
					Load('extend'); //ip 加载函数库
					$user->eip = get_client_ip();
					if($user->add()){
							$this->assign("jumpUrl","__URL__/userList/type/".$user->type);
					    $this->success("添加会员成功！");
						}else{
							$this->error('添加失败！');
						}
				}else{
					$this->error($user->getError());
				}
    }
   
    
    public function edit(){
    	
    	$user = M('User')->where('id='.$_GET['id'])->find();
    	$this->assign('user',$user);
    	
    	$this->display();
    	
    }
    
    public function editUser(){
				
				$user = D('User');
				if($user->create()){
					if(!empty($_POST['password'])){
						$user->password = md5($_POST['password']);
					}
					$this->assign("jumpUrl","__URL__/edit/id/".$_POST['id']);
					if($user->save()){
					    $this->success("修改会员成功！");
						}else{
							$this->error('修改失败！');
						}
				}else{
					$this->error($user->getError());
				}
				
    }
    
   public function doAudit()
	 {
			$model = M('User');
			if(!empty($model)) {
				$id = $_REQUEST['id'];
				if(isset($id)) {
					if($model->where('id In('.$id.')')->setField('status',1)){
						$this->assign("jumpUrl", "__URL__/userList/type/".$_GET['type']);
						$this->success('操作成功！');
					}else {
						$this->error("操作失败!");
					}
				}else {
					$this->error('非法操作');
				}
			}
	 }
	 public function doUp()
	 {
			$model = M('User');
			if(!empty($model)) {
				$id = $_REQUEST['id'];
				if(isset($id)) {
					if($model->where('id In('.$id.')')->setField('status',0)){
						$this->assign("jumpUrl", "__URL__/userList/type/".$_GET['type']);
						$this->success('操作成功！');
					}else {
						$this->error("操作失败!");
					}
				}else {
					$this->error('非法操作');
				}
			}
	 }
	 public function doTop()
	 {
	 		$value = 0;
			if($_GET['ist']){
				$value = $_GET['ist'];
			}
			$model = M('User');
			if(!empty($model)) {
				$id = $_REQUEST['id'];
				if(isset($id)) {
					if($model->where('id In('.$id.')')->setField('istop',$value)){
						$this->assign("jumpUrl", "__URL__/userList/type/".$_GET['type']);
						$this->success('操作成功！');
					}else {
						$this->error("操作失败!");
					}
				}else {
					$this->error('非法操作');
				}
			}
	 }
	 
	 public function doRecom()
	 {
			$model = M('User');
			$value = 0;
			if($_GET['ist']){
				$value = $_GET['ist'];
			}
			if(!empty($model)) {
				$id = $_REQUEST['id'];
				if(isset($id)) {
					if($model->where('id In('.$id.')')->setField('isrecom',$value)){
						$this->assign("jumpUrl", "__URL__/userList/type/".$_GET['type']);
						$this->success('操作成功！');
					}else {
						$this->error("操作失败!");
					}
				}else {
					$this->error('非法操作');
				}
			}
	 }
	 
    //删除多条记录
   public function delete()
	 {
			//删除指定记录
			$model = M('User');
			if(!empty($model)){
				$id = $_REQUEST['id'];
				if(isset($id)) {
					if($model->where('id In('.$id.')')->delete()){
						
						if($_GET['type']!='user'){
							$nextModel = M($_GET['type']);
							if(!empty($nextModel)){
								 if($nextModel->where('id In('.$id.')')->delete()){
								 		$this->assign("jumpUrl","__URL__/userList/type/".$_GET['type']);
										$this->success('删除成功！');
								 }else {
										$this->error("删除失败!");
								 }
							}else {
								$this->error('非法操作');
							}
						}else{
							$this->assign("jumpUrl","__URL__/userList/type/".$_GET['type']);
							$this->success('删除成功！');
						}
						

					}else {
						$this->error("删除失败!");
					}
				}else {
					$this->error('非法操作');
				}
			}
	 }
	 
	 
    
}
?>